package yy.robot.domain;

import org.springframework.data.domain.Page;

import java.util.List;

/**
 * Created by BaiJiFeiLong@gmail.com at 2018/5/3 18:27
 */
public class Paging<T> {
    private int pageIndex;
    private int itemsPerPage;
    private long totalItems;
    private int totalPages;
    private int currentItemCount;
    private List<T> items;

    private Paging() {
    }

    @SuppressWarnings("unchecked")
    public static <T> Paging<T> fromPage(Page<T> page) {
        Paging<T> data = new Paging();
        data.setPageIndex(page.getNumber() + 1);
        data.setItemsPerPage(page.getSize());
        data.setTotalItems(page.getTotalElements());
        data.setTotalPages(page.getTotalPages());
        data.setCurrentItemCount(page.getContent().size());
        data.setItems(page.getContent());
        return data;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getItemsPerPage() {
        return itemsPerPage;
    }

    public void setItemsPerPage(int itemsPerPage) {
        this.itemsPerPage = itemsPerPage;
    }

    public long getTotalItems() {
        return totalItems;
    }

    public void setTotalItems(long totalItems) {
        this.totalItems = totalItems;
    }

    public int getTotalPages() {
        return totalPages;
    }

    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }

    public int getCurrentItemCount() {
        return currentItemCount;
    }

    public void setCurrentItemCount(int currentItemCount) {
        this.currentItemCount = currentItemCount;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }
}

